﻿// Doc-Encode UTF8-BOM, Space(4), Unix(LF)
#include "hiobj.h"
#include "hiobj_in.h"
#include "hiobj_help.h"
#include "himpp_extension.h"
#include "klbutil/CMap.hpp"
#include <assert.h>

#include "hi_comm_vb.h"


static int map2obj_VB_CONFIG_S(hiobj_t* p_dst, klb::CMap& src)
{
    klb::CMap& r = src;
    VB_CONFIG_S* p_cfg = (VB_CONFIG_S*)p_dst->data;

    cmap_get_HI_U32(r, "MaxPoolCnt", &p_cfg->u32MaxPoolCnt);

    klb::CArray a1 = r.GetArray("CommPool");
    for (int i = 0; i < a1.Size(); i++)
    {
        klb::CMap pool = a1.GetMap(i);

        cmap_get_HI_U64(pool, "BlkSize", &p_cfg->astCommPool[i].u64BlkSize);
        cmap_get_HI_U32(pool, "BlkCnt", &p_cfg->astCommPool[i].u32BlkCnt);
        cmap_get_ENUM(pool, "RemapMode", &p_cfg->astCommPool[i].enRemapMode);
        cmap_get_HI_CHAR(pool, "MmzName", p_cfg->astCommPool[i].acMmzName, MAX_MMZ_NAME_LEN);
    }

    return 0;
}

static int obj2map_VB_CONFIG_S(klb::CMap& dst, hiobj_t* p_src)
{
    klb::CMap& r = dst;
    const VB_CONFIG_S* p_cfg = (const VB_CONFIG_S*)p_src->data;

    r["MaxPoolCnt"] = (int64_t)p_cfg->u32MaxPoolCnt;

    klb::CArray a1;
    for (int i = 0; i < VB_MAX_COMM_POOLS; i++)
    {
        if (p_cfg->astCommPool[i].u64BlkSize <= 0 || p_cfg->astCommPool[i].u32BlkCnt <= 0)
        {
            break;
        }

        klb::CMap pool;
        
        pool["BlkSize"] = (int64_t)p_cfg->astCommPool[i].u64BlkSize;
        pool["BlkCnt"] = (int64_t)p_cfg->astCommPool[i].u32BlkCnt;
        pool["RemapMode"] = (int64_t)p_cfg->astCommPool[i].enRemapMode;
        pool["MmzName"] = p_cfg->astCommPool[i].acMmzName;

        a1.Append(pool);
    }

    r["CommPool"] = a1;

    return 0;
}

//////////////////////////////////////////////////////////////////////////


int load_hiobj_vb(himpp_extension_t* p_ex)
{
    static hiobj_reg_t reg[] =
    {
        { "VB_CONFIG_S",    sizeof(VB_CONFIG_S),   map2obj_VB_CONFIG_S ,   obj2map_VB_CONFIG_S },

        { NULL, 0,  NULL, NULL}
    };

    himpp_ex_add(p_ex, reg);

    return 1;
}
